package com.kd.controller;

import com.kd.common.C_UserLoginToken;
import com.kd.common.E_UserLoginToken;
import com.kd.common.PageResult;
import com.kd.common.Result;
import com.kd.entity.Order;
import com.kd.service.order.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 订单服务实现类，继承自 MyBatis-Plus 的 ServiceImpl
 * 1.通过oid找订单
 * 2.插入订单,
 * 3.查看所有订单列表，with过滤条件   PageResult<Product>
 * 4.修改订单属性(仅老板e2可使用)
 * 5.更新订单状态 员工只能修改从1~2的修改,财务只能修改2~3，老板0~3都可以
 * 6.
 *
 */
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    OrderService orderService;


    // ===== 基础CRUD操作 =====


    // ===== 订单查询 =====
    @RequestMapping("/list/filter")
    @E_UserLoginToken
    public Result<PageResult<Order,Long>> findOrdersWithFilters(
            @RequestParam(required = false) Long cursor,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) Long oid,
            @RequestParam(required = false) Integer cid,
            @RequestParam(required = false) Integer pid,
            @RequestParam(required = false) Date startOrderDate,
            @RequestParam(required = false) Date endOrderDate,
            @RequestParam(required = false) Date startEta,
            @RequestParam(required = false) Date endEta,
            @RequestParam(required = false) BigDecimal minPrice,
            @RequestParam(required = false) BigDecimal maxPrice,
            @RequestParam(required = false) Integer status

    ) {
        try {
            PageResult<Order,Long> pageResult = orderService.findOrdersWithFilters(
                    cursor, limit, oid, cid, pid,
                    startOrderDate, endOrderDate,
                    startEta, endEta,
                    minPrice, maxPrice,
                    status);
            return Result.success(pageResult, "查询成功");
        } catch (Exception e) {
            return Result.fail(500, "查询失败: " + e.getMessage());
        }
    }

    // ===== 订单查询c =====
    @RequestMapping("/list/efilter")
    @E_UserLoginToken
    public Result<PageResult<Order,Long>> findOrdersWitheFilters(
            @RequestParam(required = false) Long cursor,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) Long oid,
            @RequestParam(required = false) Integer cid,
            @RequestParam(required = false) Integer pid


    ) {
        try {
            Integer status = 1;
            PageResult<Order,Long> pageResult = orderService.findOrdersWitheFilters(
                    cursor, limit, oid, cid, pid,

                    status);
            return Result.success(pageResult, "查询成功");
        } catch (Exception e) {
            return Result.fail(500, "查询失败: " + e.getMessage());
        }
    }

    // ===== 订单查询c =====
    @RequestMapping("/list/cfilter")
    @C_UserLoginToken
    public Result<PageResult<Order,Long>> findOrdersWithcFilters(
            @RequestParam(required = false) Long cursor,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) Long oid,
            @RequestParam(required = false) Integer cid,
            @RequestParam(required = false) Integer pid,
            @RequestParam(required=false) Integer status

    ) {
        try {
            PageResult<Order,Long> pageResult = orderService.findOrdersWithcFilters(
                    cursor, limit, oid, cid, pid,status);
            return Result.success(pageResult, "查询成功");
        } catch (Exception e) {
            return Result.fail(500, "查询失败: " + e.getMessage());
        }
    }




        @RequestMapping("/add")
        @C_UserLoginToken
        public Result<String> addProductAndOrder(@RequestBody Order order) {
            try {
                    System.out.println("🚀 接收到的订单数据:");
                    System.out.println("  pid: " + order.getPid());
                    System.out.println("  cid: " + order.getCid());
                    System.out.println("  eta: " + order.getEta());
                    System.out.println("  eta类型: " + (order.getEta() != null ? order.getEta().getClass().getName() : "null"));
                    System.out.println("  quantity: " + order.getQuantity());
                    System.out.println("  price: " + order.getPrice());
                    System.out.println("  total_amount: " + order.getTotalAmount());
                    System.out.println("  remark: " + order.getRemark());
                    System.out.println("  status: " + order.getStatus());

                    System.out.println("🔧 开始插入订单...");
                if (orderService.insertOrder(order))  {
                    return Result.success(" 订单提交成功，请联系厂家激活");
                }
                return Result.fail(" 订单提交失败");
            } catch (Exception e) {
                return Result.fail(500,  "系统错误: " + e.getMessage());
            }
        }



    @RequestMapping("/update")
    @ResponseBody
    public Result<Order> updateProduct(@RequestBody Order order) {
        try {
            orderService.updateOrder(order);
            return Result.success( order, "更新成功");
        } catch (Exception e) {
            return Result.fail(500,  "更新失败: " + e.getMessage());
        }
    }








}